package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.*;
import com.sky.service.WorkbenchService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: HuoXu
 * @Date: 2023/09/21/14:45
 * @Description: 快乐跳跳虎
 */
@Service
public class WorkbenchServiceImpl implements WorkbenchService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 获取运营数据
     * @return
     */
    @Override
    public BusinessDataVO operationalData() {
        //构建今日具体时间,获取今天的新用户
        LocalDateTime beginTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        Integer newUser=userMapper.countUser(beginTime,endTime);
        //获取当天的订单完成率(总数/完成)
        Integer orderCount = getOrderCount(beginTime, endTime, null);
        Integer validOrderCount = getOrderCount(beginTime, endTime, Orders.COMPLETED);
        Double orderCompletionRate = 0.0;
        if(orderCount != 0){
            //计算订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / orderCount;
        }
        //获取营业额
        Double amount = orderMapper.turnoverStatistics(beginTime,endTime,Orders.COMPLETED);
        amount = amount == null ? 0.0 : amount;
        //平均客单价
        Double unitPrice = 0.0;
        unitPrice=amount/validOrderCount;
        return BusinessDataVO.builder()
                .newUsers(newUser)
                .orderCompletionRate(orderCompletionRate)
                .turnover(amount)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
    }

    /**
     * 获取套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setMealMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setMealMapper.countByMap(map);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 获取菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);

        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView() {
        Map map = new HashMap();
        map.put("begin", LocalDateTime.now().with(LocalTime.MIN));
        map.put("status", Orders.TO_BE_CONFIRMED);

        //待接单
        Integer waitingOrders = orderMapper.countByMap(map);

        //待派送
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);

        //已完成
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);

        //已取消
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);

        //全部订单
        map.put("status", null);
        Integer allOrders = orderMapper.countByMap(map);

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    /**
     * 数据导出
     * @param beginTime
     * @param endTime
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime beginTime, LocalDateTime endTime) {
        Integer newUser=userMapper.countUser(beginTime,endTime);
        //获取当天的订单完成率(总数/完成)
        Integer orderCount = getOrderCount(beginTime, endTime, null);
        Integer validOrderCount = getOrderCount(beginTime, endTime, Orders.COMPLETED);
        Double orderCompletionRate = 0.0;
        if(orderCount != 0){
            //计算订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / orderCount;
        }
        //获取营业额
        Double amount = orderMapper.turnoverStatistics(beginTime,endTime,Orders.COMPLETED);
        amount = amount == null ? 0.0 : amount;
        //平均客单价
        Double unitPrice = amount == 0.0 ? 0.0 : amount/validOrderCount;
        return BusinessDataVO.builder()
                .newUsers(newUser)
                .orderCompletionRate(orderCompletionRate)
                .turnover(amount)
                .unitPrice(unitPrice)
                .validOrderCount(validOrderCount)
                .build();
    }

    private Integer getOrderCount(LocalDateTime begin, LocalDateTime end, Integer status){
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        map.put("status",status);

        return orderMapper.countByMap(map);
    }
}
